package com.itheima.practice.controller;


import com.itheima.practice.service.ChatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/ai")
public class AiServiceController {

    @Autowired
    private ChatService chatService;


    @PostMapping("/service")
    public ResponseEntity<String> handleAiService(
            @RequestParam String sessionId,
            @RequestBody String userMessage) {
        if (userMessage == null || userMessage.trim().isEmpty()) {
            return ResponseEntity.badRequest().body("用户消息不能为空");
        }
        if (sessionId == null || sessionId.trim().isEmpty()) {
            return ResponseEntity.badRequest().body("sessionId 不能为空");
        }

        try {
            String reply = chatService.chat(sessionId.trim(), userMessage.trim());
            return ResponseEntity.ok(reply);
        } catch (Exception e) {
            // 日志已由 ChatService 记录
            return ResponseEntity.status(500).body("AI服务暂时不可用，请稍后再试");
        }
    }
}